import 'package:flutter/material.dart';

/**
 * 收缩面板
 */
class ExpansionPanelDemo extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return ExpansionPanelStateDemo();
  }
}

class ExpansionPanelStateDemo extends StatefulWidget {
  @override
  State<StatefulWidget> createState() {
    return ExpansionPanelState();
  }
}

class ExpansionPanelState extends State<ExpansionPanelStateDemo> {
  List<ExpansionPanelItem> _expansionPanelItemList;

  @override
  void initState() {
    super.initState();
    _expansionPanelItemList = <ExpansionPanelItem>[
      ExpansionPanelItem(
        headText: 'Panel A',
        body: Container(
          padding: EdgeInsets.all(16.0),
          child: Text('Content for Panel A',
              style: TextStyle(
                color: Colors.blue,
              )),
          width: double.infinity,
        ),
        expaned: false,
      ),
      ExpansionPanelItem(
        headText: 'Panel B',
        body: Container(
          padding: EdgeInsets.all(16.0),
          child: Text('Content for Panel B',
              style: TextStyle(
                color: Colors.blue,
              )),
          width: double.infinity,
        ),
        expaned: false,
      ),
      ExpansionPanelItem(
        headText: 'Panel C',
        body: Container(
          padding: EdgeInsets.all(16.0),
          child: Text('Content for Panel C',
              style: TextStyle(
                color: Colors.blue,
              )),
          width: double.infinity,
        ),
        expaned: false,
      ),
    ];
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: Text('ExpansionPanelDemo'),
        elevation: 0.0,
      ),
      body: Container(
        padding: EdgeInsets.all(8.0),
        child: Column(
          mainAxisAlignment: MainAxisAlignment.center,
          children: <Widget>[
            ExpansionPanelList(
              expansionCallback: (int panelIndex, bool expaned) {
                setState(() {
                  _expansionPanelItemList[panelIndex].expaned = !expaned;
                });
              },
              children: _expansionPanelItemList.map((ExpansionPanelItem item) {
                return ExpansionPanel(
                  isExpanded: item.expaned,
                  body: item.body,
                  headerBuilder: (context, isExpansed) {
                    return Container(
                      padding: EdgeInsets.all(16.0),
                      child: Text(
                        item.headText,
                        style: TextStyle(
                          color: Colors.blue,
                        ),
                      ),
                    );
                  },
                );
              }).toList(),
            ),
          ],
        ),
      ),
    );
  }
}

class ExpansionPanelItem {
  final String headText;
  final Widget body;
  bool expaned;

  ExpansionPanelItem({this.body, this.expaned, this.headText});
}
